import json
from pprint import pprint
from google.appengine.ext import ndb
from models.models import *
from google.appengine.api import memcache
from google.appengine.ext import deferred
import xml.etree.ElementTree as etree
import logging
import webapp2
import random



class ClearDBhandler(webapp2.RequestHandler):
	def get(self):
		""" restart the database
		"""
		deferred.defer(clear_db)
		self.response.out.write("database restarted")

def clear_db():
	logging.debug("Cleaning data")
	logging.debug("Flushing memcache")
	memcache.flush_all()
	
	star_keys = Star.query().fetch(keys_only=True)
	ndb.delete_multi(star_keys)

	# deleting all from Student
	constellation_keys = Constellation.query().fetch(keys_only=True)
	ndb.delete_multi(constellation_keys)
	
	logging.debug("Database cleared")

	


class InitDBhandler(webapp2.RequestHandler):
	def get(self):
		""" restart the database
		"""
		deferred.defer(init_db)
		self.response.out.write("database created")
		

def init_db():
	for i in range(500):
	# for i in range(3500):
		s = Star(id='st'+str(i))
		s.x = random.randint(0,5000)
		s.y = random.randint(0,1715)
		s.put()

	logging.debug("Stars created")